package com.blog.controller.admin;

import com.blog.entity.Blogger;
import com.blog.service.BloggerService;
import com.blog.util.Const;
import com.blog.util.CryptographyUtil;
import com.blog.util.DateUtil;
import com.blog.util.ResponseUtil;
import net.sf.json.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;

/**
 * @author 师晓峰
 * @version V1.0
 * @date 2023/9/10 16:01
 * @Description:
 *  博主信息控制类
 */
@Controller
@RequestMapping({"/admin/blogger"})
public class BloggerAdminController {



    @Resource
    private BloggerService bloggerService;

    @RequestMapping({"/save"})
    public String save(@RequestParam("imageFile")MultipartFile imageFile, Blogger blogger,
                       HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 处理头像信息
        if(!imageFile.isEmpty()){
            // 获取当前项目路径
            String filePath = request.getServletContext().getRealPath("/");
            // 获取文件名
            String imageName = DateUtil.getCurrentDateStr() + "." + imageFile.getOriginalFilename().split("\\.")[1];
            System.out.println(filePath);
            imageFile.transferTo(new File(filePath + "static/userImages/"+imageName));
            blogger.setImageName(imageName);
        }

        Integer resultTotal = bloggerService.update(blogger);
        StringBuffer result = new StringBuffer();
        if (resultTotal > 0){
            result.append("<script language='javascript'> alert('修改成功');</script>");
        }else{
            result.append("<script language='javascript'> alert('修改失败');</script>");
        }

        ResponseUtil.write(response, result);
        return null;
    }


    /**
     *  获取博主的Json格式
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping({"/find"})
    public String find(HttpServletResponse response) throws Exception {
        Blogger blogger = (Blogger) SecurityUtils.getSubject().getSession().getAttribute(Const.CURRENT_USER);
        JSONObject jsonObject = JSONObject.fromObject(blogger);
        ResponseUtil.write(response, jsonObject);
        return null;
    }


    @RequestMapping({"/modifyPassword"})
    public String modifyPassword(@RequestParam("id") String id,@RequestParam("newPassword") String newPassword,HttpServletResponse response) throws Exception {
        Blogger blogger = new Blogger();
        blogger.setId(Integer.parseInt(id));
        blogger.setPassword(CryptographyUtil.md5(newPassword,Const.SALT));
        int resultTotal = bloggerService.update(blogger);

        JSONObject result = new JSONObject();
        if (resultTotal > 0){
            result.put("success",Boolean.TRUE);
        }else{
            result.put("success",Boolean.FALSE);
        }
        ResponseUtil.write(response, result);
        return null;
    }

    /**
     * 用户退出功能
     * @return
     */
    @RequestMapping({"/logout"})
    public String logout(){
        SecurityUtils.getSubject().logout();
        return "redirect:/login.jsp";
    }



}
